<template>
  <div class="app-container">
    <div class="head-container"></div>
    <div v-if="!crud.props.searchToggle">
      <el-form size="small" label-width="70px">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
              <el-form-item label="客户名称">
                <table-select
                  :tableColumns="
                  [
                  {prop:'code',label:'客户编码',width: '100'},
                  {prop:'name',label:'客户名称',width: '150'},
                  {prop:'address',label:'地址'},
                  {prop:'sCustomerName',label:'二批'},
                  ]"
                  labelKey="name"
                  valueKey="id"
                  width="700"
                  :value.sync="query.customerIds"
                  placeholder="请选择客户"
                  url="api/dropDown/basCustomerList"
                ></table-select>
              </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="商品品牌">
              <table-select
                :tableColumns="[
                  { prop: 'code', label: '品牌编码', width: '100' },
                  { prop: 'name', label: '品牌名称', width: '280' },
                ]"
                :value.sync="query.goodsBrandIds"
                placeholder="请选择品牌"
                url="/api/dropDown/basGoodsBrandList"
              ></table-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="商品名称">
              <table-select
                :tableColumns="[
                  { prop: 'goodsCode', label: '商品编码', width: '100' },
                  { prop: 'goodsName', label: '商品名称', width: '280' },
                ]"
                labelKey="goodsName"
                valueKey="goodsId"
                :value.sync="query.goodsIds"
                placeholder="请选择商品"
                url="/api/dropDown/basGoodsListForAdd"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="商品分类">
              <treeselect
                v-model="goodsTypeIds"
                :options="typeList"
                placeholder="商品"
                style="width: 100%"
                :normalizer="normalizer"
                multiple
                @input="getgoodsType"
              />
            </el-form-item>
          </el-col>
          </el-row>
       
        <el-row class="flex">
          <el-button
            size="mini"
            type="primary"
            @click="queryReport"
            >查询</el-button>
        <el-button
            size="mini"
            type="primary"
            @click="resetQuery"
            style="margin-right:5px">重置</el-button> 
        </el-row>
      </el-form>
    </div>
<!--工具栏-->
<crudOperation :permission="permission" />
    <!--表格渲染-->
    <el-table
      ref="table"
      border
      stripe
      v-loading="crud.loading"
      :data="crud.data"
      size="small"
      style="width: 100%"
      height="700"
      @row-click="crud.selectRow"
      @selection-change="crud.selectionChangeHandler"
      @row-dblclick="getdetial">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="55" label="序号" />
      <el-table-column label="客户编码" prop="customerCode"/>
    <el-table-column label="客户名称" prop="customerName"/>
     <el-table-column label="商品编码" prop="goodsCode"/>
      <el-table-column label="商品名称" prop="goodsName"/>
      <el-table-column label="条形码" prop="barCode"/>
      <el-table-column label="商品品牌" prop="goodsBrandName"/>
      <el-table-column label="商品分类" prop="goodsTypeName"/>
      <el-table-column label="规格" prop="model"/>
      <el-table-column label="辅数量" prop="mergeGoodsCount"/>
      <el-table-column label="数量" prop="goodsCount"/>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import crudOperation from "@crud/CRUD.operation";
import pagination from "@crud/Pagination";
import Treeselect from "@riophae/vue-treeselect";
import { getBasGoodsTypeTree } from "@/api/erp/basGoodsType";
// crud交由presenter持有
const defaultCrud = CRUD({
  title: "客户库存统计",
  optShow: {
    add: false,
  },
  url: "/api/customerStock",
  sort: "id,desc",
});
const defaultForm = {};
export default {
  name: "CustomerStock",
  components: {
    pagination,
    crudOperation,
    Treeselect,
  },
  mixins: [
    presenter(defaultCrud),
    header(),
    form(defaultForm),
    crud(),
  ],
  mounted() {
  },
  created() {
    
    getBasGoodsTypeTree({ size: 999 }).then((res) => {
      this.typeList = res.data;
    });
  },
  data() {
    return {
      goodsBrandCode: " ",
      customerAreaIds: [],
      goodsTypeIds: [],
      goodsBrandName: " ",
      permission: {
        add: ["admin", "customerStock:add"],
      },
      rules: {},
      typeList: [],
    };
  },
  watch: {
      goodsTypeIds(n) {
      if (!n.length) {
        this.query.goodsTypeIds = null;
      }
    },
  },
  methods: {
    getdetial(row){
      console.log(row,'00000000000000000')
      this.$router.push({
        path: "/sale/CustomerStockDetail/"+ row.id,
      });
    },
      // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {},
      getgoodsType(val) {
      let str = ''
      val.forEach(item=>{
        const itemNode = this.getTreeName(this.typeList,item)
        str = this.getNode(itemNode,str)
      })
      this.query.goodsTypeIds = str
      this.query.goodsTypeIds = this.query.goodsTypeIds.length
        ? this.query.goodsTypeIds.join(",")
        : "";
    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children == null || node.children.length == 0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    getNode(node, str) {
      if (node.children) {
        for (let i = 0; i < node.children.length; i++) {
          str = this.getNode(node.children[i], str);
        }
      }
      return (str += node.id + ",");
    },
    
    resetQuery() {
      this.customerAreaIds = [];
      this.customerTypeIds = [];
      this.goodsTypeIds = [];
      this.crud.resetQuery();
    },
    queryReport() {
      this.crud.toQuery();
    },
    getNoStr(val,type,prop){
      let str = ''
      val.forEach(item=>{
        const itemNode = this.getTreeName(this[type],item)
        str = this.getNode(itemNode,str)
      })
      this.query[prop] = str
    },
    getTreeName(treeList,id){
      let _this=this
      for (let i = 0; i < treeList.length; i++) {
        let treeItem=treeList[i]
        if(treeItem.id===id){
          return treeItem
        }else{
          if(treeItem.children && treeItem.children.length>0){
            let res=_this.getTreeName(treeItem.children,id)
            if(res){
              return res
            }
          }
        }
      }
    },
  },
};
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
.flex {
  display: flex;
  justify-content: center;
}
::v-deep .el-table--border th.el-table__cell {
  border-bottom: 1px solid #000;
}
</style>
